<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="filters">
                <el-form-item>
                    <el-input v-model="filters.ip" placeholder="IP查询" clearable></el-input>
                </el-form-item>
                <el-form-item>
                    <el-tooltip :content="'RT模式: ' + filters.type" placement="top">
                        <el-switch
                            v-model="filters.type"
                            active-color="#13ce66"
                            inactive-color="#ff4949">
                        </el-switch>
                    </el-tooltip>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" v-on:click="getProxyList">查询</el-button>
                </el-form-item>

            </el-form>
        </el-col>

        <!--列表-->
        <el-table :data="proxyList" stripe highlight-current-row v-loading="listLoading" @selection-change="selsChange"
                  :row-class-name="tableRowClassName"
                  style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column prop="ip" label="IP" width="120">
            </el-table-column>
            <el-table-column prop="port" label="端口" width="80">
            </el-table-column>
            <el-table-column prop="status" label="状态" width="80" :formatter="formatStatus">
            </el-table-column>
            <el-table-column prop="location" label="位置" width="120">
            </el-table-column>
            <el-table-column prop="type" label="类型" >
            </el-table-column>
            <el-table-column prop="spread" label="速度" >
            </el-table-column>
            <el-table-column prop="verifyCount" label="验证次数" >
            </el-table-column>
            <el-table-column prop="availableCount" label="可用次数" width="100" sortable>
            </el-table-column>
            <el-table-column prop="unavailableCount" label="不可用次数" min-width="120" sortable>
            </el-table-column>
            <el-table-column prop="availableRate" label="可用比" width="100" sortable>
            </el-table-column>
            <el-table-column prop="createTime" label="创建时间" width="140" :formatter="formatDate" sortable>
            </el-table-column>
            <el-table-column prop="lastValidate" label="最后验证" width="140" :formatter="formatDate" sortable>
            </el-table-column>

            <el-table-column label="操作" width="60" fixed="right" style="padding-right: 2px">
                <template slot-scope="scope">
                    <el-button type="danger" size="mini" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" v-on:click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="page"
                :page-sizes="[20, 50, 100]"
                :page-size="20"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                style="float:right;">
            </el-pagination>
        </el-col>

        <!--编辑界面-->
        <el-dialog title="编辑" :visible.sync="editFormVisible" :close-on-click-modal="false">
            <el-form :model="editForm" label-width="80px" ref="editForm">
                <el-form-item label="姓名" prop="name">
                    <el-input v-model="editForm.name" auto-complete="off"></el-input>
                </el-form-item>

                <el-form-item label="性别">
                    <el-radio-group v-model="editForm.sex">
                        <el-radio class="radio" :label="1">男</el-radio>
                        <el-radio class="radio" :label="0">女</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="花名">
                    <el-input v-model="editForm.nickname"></el-input>
                </el-form-item>
                <el-form-item label="邮箱">
                    <el-input v-model="editForm.email"></el-input>
                </el-form-item>
                <el-form-item label="电话" prop="telephone">
                    <el-input v-model="editForm.telephone"></el-input>
                </el-form-item>
                <el-form-item label="生日">
                    <el-date-picker type="date" placeholder="选择日期" v-model="editForm.birthday"></el-date-picker>
                </el-form-item>
                <el-form-item label="地址">
                    <el-input type="textarea" v-model="editForm.address"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="editFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
            </div>
        </el-dialog>

    </section>
</template>

<script>
    import util from '../../common/utils'
    import {proxyList} from '../../api/api';
    import {getTime} from '../../api/commonUtil';

    export default {
        data() {
            return {
                filters: {
                    type: true,
                    ip: ''
                },
                proxyList: [],
                total: 0,
                page: 1,
                pageSize: 20,
                listLoading: false,
                sels: [],//列表选中列

                editFormVisible: false,//编辑界面是否显示
                editLoading: false,
                //编辑界面数据
                editForm: {
                    name: '',
                    sex: -1,
                    nickname: '',
                    email: '',
                    birthday: '',
                    address: ''
                },
                addFormVisible: false,//新增界面是否显示
                addLoading: false,
                formLabelWidth: '80px',
                //新增界面数据
                addForm: {
                    name: '',
                    password: '1',
                    nickname: '花花',
                    sex: -1,
                    email: 'default@qq.com',
                    birthday: '',
                    address: 'default address'
                }
            }
        },
        methods: {
            // 隔行换色
            tableRowClassName({row, rowIndex}) {
                if (rowIndex === 1) {
                    return 'warning-row';
                } else if (rowIndex === 3) {
                    return 'success-row';
                }
                return '';
            },
            formatStatus: function (row, column) {
                return row.status == 0 ? '可用' : row.status == 0 ? '不可用' : '未验证';
            },
            formatDate: function (row, column, cellValue, index) {
                const daterc = row[column.property]
                return daterc == null ? 'null' : getTime(daterc);
            },
            // 切换pageSize
            handleSizeChange(val) {
                this.pageSize = val;
                this.getProxyList();
            },
            // 换页
            handleCurrentChange(val) {
                this.page = val;
                this.getProxyList();
            },

            //获取代理列表
            getProxyList() {
                let params = {
                    ip: this.filters.ip ? null : '',
                    type: this.filters.type ? 'rt' : '',
                    currentPage: this.page,
                    pageSize: this.pageSizes == null ? 20 : this.pageSize,
                };
                this.listLoading = true;
                proxyList(params).then((res) => {
                    if (res != undefined&&res.status ) {
                        this.$message({
                            message: '加载成功',
                            type: 'success'
                        });
                        if (res.data !== undefined && res.data !== null) {
                            this.total = parseInt(res.data.totalNum);
                            this.proxyList = res.data.itemList;
                        } else {
                            this.total = 0;
                            this.proxyList = [];
                        }
                    } else {
                        this.listLoading = false;
                        this.$message({
                            message: '加载失败',
                            type: 'error'
                        });
                    }
                    this.listLoading = false;
                });
            },
            //删除
            handleDel: function (index, row) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    debugger;
                    var userId = {
                        "userId": row.userId
                    };
                    removeUser(userId).then((res) => {
                        this.listLoading = false;
                        if (res.status) {
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                        } else {
                            this.$message({
                                message: '删除失败',
                                type: 'warning'
                            });
                        }
                        this.getProxyList();
                    });
                }).catch(() => {
                    this.listLoading = false;
                    this.$message({
                        message: '删除失败',
                        type: 'warning'
                    });
                });
            },
            //显示编辑界面
            handleEdit: function (index, row) {
                this.editFormVisible = true;
                this.editForm = Object.assign({}, row);
            },
            //显示新增界面
            handleAdd: function () {
                this.addFormVisible = true;
                this.addForm = {
                    name: '',
                    sex: -1,
                    birthday: '',
                    address: ''
                };
            },
            //编辑
            editSubmit: function () {
                this.$refs.editForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.editLoading = true;
                            let para = Object.assign({}, this.editForm);
                            para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
                            editUser(para).then((res) => {
                                this.editLoading = false;
                                this.$message({
                                    message: '提交成功',
                                    type: 'success'
                                });
                                this.$refs['editForm'].resetFields();
                                this.editFormVisible = false;
                                this.getProxyList();
                            });
                        });
                    }
                });
            },
            // 用户新增
            addSubmit: function () {
                this.$refs.addForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.addLoading = true;
                            let para = Object.assign({}, this.addForm);
                            para.birthday = (!para.birthday || para.birthday == '') ? '' : util.formatDate.format(new Date(para.birthday), 'yyyy-MM-dd');
                            debugger;
                            addUser(para).then((res) => {
                                debugger;
                                this.addLoading = false;
                                if (res.status) {
                                    this.$message({
                                        message: '提交成功',
                                        type: 'success'
                                    });
                                    this.$refs['addForm'].resetFields();
                                    this.addFormVisible = false;
                                } else {
                                    this.$message({
                                        message: '提交失败',
                                        type: 'error'
                                    });
                                }
                                this.getProxyList();
                            });
                        });
                    }
                });
            },
            // 多选
            selsChange: function (sels) {
                this.sels = sels;
            },
            //批量删除
            batchRemove: function () {
                var ids = this.sels.map(item => item.userId).toString();
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    let param = {userIds: ids};
                    console.log(ids);
                    batchRemoveUser(param).then((res) => {
                        this.listLoading = false;
                        if (res.status) {
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                        } else {
                            this.$message({
                                message: '删除失败',
                                type: 'warning'
                            });
                        }
                        this.getProxyList();
                    });
                }).catch(() => {
                    this.listLoading = false;

                });
            }
        },
        mounted() {
            this.getProxyList();
        },
        created() {
            this.getProxyList()
        },
    }

</script>

<style scoped>
    .el-table .warning-row {
        background: oldlace;
    }

    .el-table .success-row {
        background: #f0f9eb;
    }

</style>
